//
//  JKMineVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/23.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKMineVC.h"

// vc
#import "ByUIMineSettingsVC.h"

// view
#import "JKViewMineHeader.h"            // 头视图
#import "JKViewMineSections.h"          // 板块

// api
#import "ByApiMine.h"

@interface JKMineVC ()

@property (nonatomic, strong) JKViewMineHeader *header;
@property (nonatomic, strong) JKViewMineSections *sectionsView;
@end

@implementation JKMineVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的推广";
    
    [[self.view viewWithTag:888] removeFromSuperview];
    
    [self addBYItemviews:@[@"我的-设置"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    
    [self createViews];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
    
    ByModelUser *user = [ByCacheUser getUserInfo];
    _header.model = user;
    if (user) {
        [self fetchData];
        [self fetchCountInfo];
    } else {
        user.personCount = @"0";
        [ByCacheUser setUserInfo:user];
    }
}

#pragma mark -- 获取数据
// 获取积分
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.code_type = BY_USER.account_id;
    [ByApiMine getMinePoints:model success:^(ByNetBaseResult *result) {
        ByModelUser *user = [ByCacheUser getUserInfo];
        user.points = [result.data objectForKey:@"score1"];
        [ByCacheUser setUserInfo:user];
        _header.model = user;
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}
// 获取自身统计
- (void)fetchCountInfo{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.account_id = BY_USER.uid;
    model.account_type = @"6";
    [ByApiMine fetchCountInfo:model success:^(ByNetBaseResult *result) {
        ByModelUser *user = [ByCacheUser getUserInfo];
        user.personCount = result.data[@"member_count"];
        [ByCacheUser setUserInfo:user];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}

#pragma mark - 创建视图
- (void)createViews {
    
    _header = [JKViewMineHeader new];
    [self.view addSubview:_header];
    [_header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
    }];
    
    _sectionsView = [JKViewMineSections new];
    [self.view addSubview:_sectionsView];
    [_sectionsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(_header.mas_bottom).offset(30);
    
    }];
}

#pragma mark - 事件
- (void)rightItemsAction:(NSInteger)rightIndex {
    
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return;
    }
    ByUIMineSettingsVC *vc = [[ByUIMineSettingsVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}


@end
